package cn.cstech.aijia.model;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

/**
 * Created with Android Studio.
 * Title:UserInfo
 * Description:
 * Copyright:Copyright (c) 2014
 * Company:中国平安健康保险有限公司
 * Author:Alice
 * Date:15/9/2
 * Time:下午1:02
 * Version 1.0
 */
public class UserInfo extends BaseDataHeader implements Serializable {
    private static final long serialVersionUID = -138307633996374153L;
    //用户昵称
    public String nickName;
    //头像
    public String icon;
    //地址
    public String address;
    //用户 id
    public String userId;
    //手机号
    public String phoneNum;

    public static UserInfo deserialize(String json) throws JSONException {
        if (json != null && !json.isEmpty()) {
            return deserialize(new JSONObject(json));
        }
        return null;
    }

    public static UserInfo deserialize(JSONObject json) throws JSONException {
        if (json != null && json != JSONObject.NULL && json.length() > 0) {
            UserInfo result = new UserInfo();

            result.message = json.optString("message");
            result.state = json.optInt("state");
            json = json.optJSONObject("data");

            if(json != null) {

                if (!json.isNull("nickName")) {
                    result.nickName = json.optString("nickName", null);
                }

                if (!json.isNull("icon")) {
                    result.icon = json.optString("icon", null);
                }

                if (!json.isNull("address")) {
                    result.address = json.optString("address", null);
                }

                if (!json.isNull("userId")) {
                    result.userId = json.optString("userId", null);
                }

                if (!json.isNull("phoneNum")) {
                    result.phoneNum = json.optString("phoneNum", null);
                }
            }
            return result;
        }
        return null;
    }
}
